class Solution {
public:
    bool stoneGame(vector<int>& piles) {
        int n = piles.size();
        vector<vector<int>> dp(n+2,vector<int>(n+2));
        
        for(int len = 1;len <= n;len++){
            for(int i = 1;i+len-1 <= n;i++){
                int j = i+len-1;
                dp[i][j] = piles[i-1] - dp[i+1][j];
                dp[i][j] = max(dp[i][j],piles[j-1] - dp[i][j-1]);
            }
        }
        
        return dp[1][n] > 0;
    }
};
